package org.example.travel_web.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.travel_web.dto.PageResult;
import org.example.travel_web.dto.attr.AttrPageReq;
import org.example.travel_web.dto.attr.AttrPageResp;
import org.example.travel_web.dto.attr.AttrReq;
import org.example.travel_web.entity.AttractionsET;
import org.example.travel_web.entity.NewsET;
import org.example.travel_web.entity.RoutesET;
import org.example.travel_web.entity.UserAttrET;
import org.example.travel_web.exception.RunException;
import org.example.travel_web.mapper.UserAttrMapper;
import org.example.travel_web.service.UserAttrService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 23591
 * @description 针对表【travel_user_attr(用户收藏)】的数据库操作Service实现
 * @createDate 2025-01-21 15:58:27
 */
@Service
public class UserAttrServiceImpl extends ServiceImpl<UserAttrMapper, UserAttrET>
        implements UserAttrService {

    @Override
    public PageResult<AttrPageResp> pageResult(AttrPageReq req) {
        MPJLambdaWrapper<UserAttrET> wrapper = new MPJLambdaWrapper<UserAttrET>()
                .selectAll(UserAttrET.class);
        wrapper.eq(UserAttrET::getUid, StpUtil.getLoginIdAsLong());
        wrapper.eq(UserAttrET::getType, req.getType());
        switch (req.getType()) {
            case ROUTE -> {
                wrapper.selectAll(RoutesET.class);
                wrapper.selectAs(RoutesET::getTitle, AttrPageResp::getName);
                wrapper.rightJoin(RoutesET.class, RoutesET::getRouteId, UserAttrET::getRelateId);
            }
            case NEWS -> {
                wrapper.selectAll(NewsET.class);
                wrapper.selectAs(NewsET::getTitle, AttrPageResp::getName);
                wrapper.rightJoin(NewsET.class, NewsET::getArticleId, UserAttrET::getRelateId);
            }
            case ATTRACTION -> {
                wrapper.selectAll(AttractionsET.class);
                wrapper.rightJoin(AttractionsET.class, AttractionsET::getAttractionId, UserAttrET::getRelateId);
            }
        }
        Page<AttrPageResp> page = getBaseMapper().selectJoinPage(new Page<>(req.getPage(), req.getSize()), AttrPageResp.class, wrapper);
        return PageResult.of(page.getTotal(), page.getCurrent(), page.getSize(), page.getRecords());
    }

    @Override
    public void add(AttrReq req) {
        long userId = StpUtil.getLoginIdAsLong();
        LambdaQueryWrapper<UserAttrET> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAttrET::getUid, userId)
                .eq(UserAttrET::getType, req.getType())
                .eq(UserAttrET::getRelateId, req.getRelateId());
        if (exists(wrapper)) {
            throw new RunException("已收藏");
        }
        UserAttrET userAttrET = new UserAttrET();
        userAttrET.setUid(userId);
        userAttrET.setType(req.getType().name());
        userAttrET.setRelateId(req.getRelateId());
        if (!save(userAttrET)) {
            throw new RunException("收藏失败");
        }
    }

    @Override
    public void cancel(AttrReq req) {
        LambdaQueryWrapper<UserAttrET> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAttrET::getType, req.getType())
                .eq(UserAttrET::getRelateId, req.getRelateId())
                .eq(UserAttrET::getUid, StpUtil.getLoginIdAsLong());
        if (!remove(wrapper)) {
            throw new RunException("取消收藏失败");
        }
    }

    @Override
    public void delete(Long[] ids) {
        LambdaQueryWrapper<UserAttrET> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserAttrET::getId, List.of(ids))
                .eq(UserAttrET::getUid, StpUtil.getLoginIdAsLong());
        if (!remove(wrapper)) {
            throw new RunException("删除失败");
        }
    }
}




